package com.neiquan.weiguan.presenter;

import android.content.Context;
import android.database.SQLException;

import com.neiquan.weiguan.bean.ChannelItem;
import com.neiquan.weiguan.db.SQLHelper;
import com.neiquan.weiguan.fragment.view.EditMenuFragmentView;
import com.neiquan.weiguan.fragment.view.HomeTabFragmentView;
import com.neiquan.weiguan.fragment.view.MenuFragmentView;
import com.neiquan.weiguan.zip.ChannelZip;
import com.neiquan.weiguan.zip.impl.ChannelZipImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wyl on 2016/6/20.
 * 栏目业务逻辑
 */
public class ChannelPresenter {
    private ChannelZip channelZip;
    private HomeTabFragmentView homeTabFragmentView;
    private EditMenuFragmentView editMenuFragmentView;
    private MenuFragmentView menuFragmentView;

    public ChannelPresenter(Context context, HomeTabFragmentView homeTabFragmentView) throws SQLException {
        this.homeTabFragmentView = homeTabFragmentView;
        if (channelZip == null)
            channelZip = new ChannelZipImpl(context);
    }

    public ChannelPresenter(Context context, EditMenuFragmentView editMenuFragmentView) throws SQLException {
        this.editMenuFragmentView = editMenuFragmentView;
        if (channelZip == null)
            channelZip = new ChannelZipImpl(context);
    }

    public ChannelPresenter(Context context, MenuFragmentView menuFragmentView) throws SQLException {
        this.menuFragmentView = menuFragmentView;
        if (channelZip == null)
            channelZip = new ChannelZipImpl(context);
    }

    public ChannelPresenter(Context context) {
        if (channelZip == null)
            channelZip = new ChannelZipImpl(context);
    }

    /**
     * 清除所有的频道
     */
    public void deleteAllChannel() {
        channelZip.clearFeedTable();
    }

    /**
     * 获取用户的频道
     *
     * @return 数据库存在用户配置 ? 数据库内的用户选择频道 : 默认用户选择频道 ;
     */
    public List<ChannelItem> getUserChannel() {
        List<Map<String, String>> cacheList = channelZip.listCache(SQLHelper.SELECTED + "= ?", new String[]{"1"});
        List<ChannelItem> list = new ArrayList<ChannelItem>();
        if (cacheList != null && !((List) cacheList).isEmpty()) {
            int count = cacheList.size();
            for (int i = 0; i < count; i++) {
                ChannelItem navigate = new ChannelItem();
                navigate.setId(Integer.valueOf(cacheList.get(i).get(SQLHelper.ID)));
                navigate.setName(cacheList.get(i).get(SQLHelper.NAME));
                navigate.setOrderId(Integer.valueOf(cacheList.get(i).get(SQLHelper.ORDERID)));
                navigate.setSelected(Integer.valueOf(cacheList.get(i).get(SQLHelper.SELECTED)));
                list.add(navigate);
            }
        }
        if (homeTabFragmentView != null)
            homeTabFragmentView.getMenus(list);
        if (editMenuFragmentView != null)
            editMenuFragmentView.getUserMenus(list);
        return list;

    }

    /**
     * 获取所有频道
     *
     * @return
     */
    public List<ChannelItem> getAllChannel() {
        List<ChannelItem> userChannelItems = getUserChannel();
        List<ChannelItem> otherChannelItems = getOtherChannel();
        userChannelItems.addAll(otherChannelItems);
        if (menuFragmentView != null)
            menuFragmentView.getMenusList(userChannelItems);
        return userChannelItems;
    }

    /**
     * 获取其他的频道
     *
     * @return 数据库存在用户配置 ? 数据库内的其它频道 : 默认其它频道 ;
     */
    public List<ChannelItem> getOtherChannel() {
        Object cacheList = channelZip.listCache(SQLHelper.SELECTED + "= ?", new String[]{"0"});
        List<ChannelItem> list = new ArrayList<ChannelItem>();
        if (cacheList != null && !((List) cacheList).isEmpty()) {
            List<Map<String, String>> maplist = (List) cacheList;
            int count = maplist.size();
            for (int i = 0; i < count; i++) {
                ChannelItem navigate = new ChannelItem();
                navigate.setId(Integer.valueOf(maplist.get(i).get(SQLHelper.ID)));
                navigate.setName(maplist.get(i).get(SQLHelper.NAME));
                navigate.setOrderId(Integer.valueOf(maplist.get(i).get(SQLHelper.ORDERID)));
                navigate.setSelected(Integer.valueOf(maplist.get(i).get(SQLHelper.SELECTED)));
                list.add(navigate);
            }
        }
        if (editMenuFragmentView != null)
            editMenuFragmentView.getOtherMenus(list);
        return list;
    }

    /**
     * 保存用户频道到数据库
     *
     * @param userList
     */
    public void saveUserChannel(List<ChannelItem> userList) {
        for (int i = 0; i < userList.size(); i++) {
            ChannelItem channelItem = userList.get(i);
            channelItem.setOrderId(i);
            channelItem.setSelected(Integer.valueOf(1));
            channelZip.addCache(channelItem);
        }
    }

    /**
     * 保存其他频道到数据库
     *
     * @param otherList
     */
    public void saveOtherChannel(List<ChannelItem> otherList) {
        for (int i = 0; i < otherList.size(); i++) {
            ChannelItem channelItem = otherList.get(i);
            channelItem.setOrderId(i);
            channelItem.setSelected(Integer.valueOf(0));
            channelZip.addCache(channelItem);
        }
    }

    /**
     * 初始化数据库内的频道数据
     */
    public void initDefaultChannel() {
        deleteAllChannel();
        List<ChannelItem> defaultUserChannels = new ArrayList<ChannelItem>();
        List<ChannelItem> defaultOtherChannels = new ArrayList<ChannelItem>();
        defaultUserChannels.add(new ChannelItem(1, "推荐", 1, 1));
        defaultUserChannels.add(new ChannelItem(2, "热点", 2, 1));
        defaultUserChannels.add(new ChannelItem(3, "杭州", 3, 1));
        defaultUserChannels.add(new ChannelItem(4, "时尚", 4, 1));
        defaultUserChannels.add(new ChannelItem(5, "科技", 5, 1));
        defaultUserChannels.add(new ChannelItem(6, "体育", 6, 1));
        defaultUserChannels.add(new ChannelItem(7, "军事", 7, 1));
        defaultOtherChannels.add(new ChannelItem(8, "财经", 1, 0));
        defaultOtherChannels.add(new ChannelItem(9, "汽车", 2, 0));
        defaultOtherChannels.add(new ChannelItem(10, "房产", 3, 0));
        defaultOtherChannels.add(new ChannelItem(11, "社会", 4, 0));
        defaultOtherChannels.add(new ChannelItem(12, "情感", 5, 0));
        defaultOtherChannels.add(new ChannelItem(13, "女人", 6, 0));
        defaultOtherChannels.add(new ChannelItem(14, "旅游", 7, 0));
        defaultOtherChannels.add(new ChannelItem(15, "健康", 8, 0));
        defaultOtherChannels.add(new ChannelItem(16, "美女", 9, 0));
        defaultOtherChannels.add(new ChannelItem(17, "游戏", 10, 0));
        defaultOtherChannels.add(new ChannelItem(18, "数码", 11, 0));
        defaultUserChannels.add(new ChannelItem(19, "娱乐", 12, 0));
        saveUserChannel(defaultUserChannels);
        saveOtherChannel(defaultOtherChannels);
    }
}
